package com.yyge.common.controller;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyge.controller.admin.mapstruct.BeanConverter;
import com.yyge.callback.PageCallback;
import com.yyge.common.enums.AckCode;
import com.yyge.common.util.FieldUtil;
import com.yyge.common.vo.resp.PageResp;
import com.yyge.common.vo.resp.R;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
public class BaseController {

    @Autowired
    private FieldUtil fieldUtil;

    public QueryWrapper getWrapper(Object object, QueryWrapper wrapper) throws Exception {
        //获取实体所有对应的列
        Map<String, ColumnCache> columnMap = LambdaUtils.getColumnMap(object.getClass());
        for (Field field : fieldUtil.getFieldNames(object)) {
            Class<?> Obj = object.getClass();

            if (field.getType().equals(String.class)) {
                String MethodName = "get" + (field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1));

                // 获取方法
                Method method = Obj.getMethod(MethodName);
                // 调用方法
                Object result = method.invoke(object);
                String column = columnMap.get(LambdaUtils.formatKey(field.getName())).getColumn();
                wrapper.like(column, result);
            }
        }
        return wrapper;
    }

    public PageResp page(Integer page, Integer size, PageCallback callback){
      Page pageInfo = new Page<>();
      page = page==null ? 1:page;
      size = size==null ? 10:size;
      pageInfo.setCurrent(page);
      pageInfo.setSize(size);
      pageInfo = callback.doPage(pageInfo);

      PageResp pageVo= BeanConverter.MAPPER.page2vo(pageInfo);
      return pageVo;
    }


   public R toR(int value){
        return value>0? R.build(AckCode.SUCCESS): R.build(AckCode.DATABASE_ERROR);
    }

    public R toR(boolean value){
        return value? R.build(AckCode.SUCCESS): R.build(AckCode.DATABASE_ERROR);
    }


    /**
     * 导出集合数据到excel表中
     * @param response
     * @param cls
     * @param arrList
     * @param <T>
     * @throws IOException
     */
    public <T> void writeListToExcel(HttpServletResponse response, Class<T> cls, List<T> arrList) throws IOException {
        //设定输出到浏览器的格式   .xlsx 对应的 mime-type类型
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("UTF-8");
        // 文件名
        String fileName = System.currentTimeMillis() + ".xlsx";
        //设定给浏览器，告诉响应给浏览器文件名
        response.setHeader("Content-disposition", "attachment;filename="+fileName);

        EasyExcel.write(response.getOutputStream(), cls).sheet("内容").doWrite(arrList);
    }
}
